

import { registerClass } from "../../../cfw/tools/class_decorator";
import BaseAd from "../../core/base/BaseAd";
import { SDKDir, SDKLogicState, SDKState } from "../../SDKDefine";

//https://ie-activity-cn.heytapimage.com/static/minigame/CN/docs/index.html#/develop/ad/banner-ad
/**
 * 1051
 * 创建 Banner 广告组件，如果已经创建过 Banner 广告组件，则会使用已创建的广告组件对象
 */
@registerClass('OppoBannerAd')
export default class OppoBannerAd extends BaseAd {

    show(): void {
        if (this.ad) {
            this.ad.show();
        }
    }

    // preload(s: SDKState) {
    //     this.setLogicState(s)
    //     this.destroy();
    //     this.create();
    //     this.show()
    // }

    open(): void {
        this.create();
        this.show()
    }

    updateSize() {
        if (!this.ad) {
            return;
        }
        let winSize = this.sdk.getSystemInfoSync();
        if (this.style.width != 0 && this.style.height != 0 && this.ad) {
            switch (this.param.dir) {
                case SDKDir.BOTTOM_LEFT:
                    this.ad.style.top = (winSize.windowHeight - this.style.height);
                    this.ad.style.left = 0;
                    break;
                case SDKDir.BOTTOM_MID:
                    this.ad.style.top = (winSize.windowHeight - this.style.height);
                    this.ad.style.left = (winSize.windowWidth - this.style.width) / 2;
                    break;
                case SDKDir.RIGHT_MID:
                    this.ad.style.top = (winSize.windowHeight - this.style.height) / 2;
                    this.ad.style.left = winSize.windowWidth - this.style.width;
                    break;
                case SDKDir.MID:
                    this.ad.style.top = (winSize.windowHeight - this.style.height) / 2;
                    this.ad.style.left = (winSize.windowWidth - this.style.width) / 2;
                    break;
                case SDKDir.TOP_MID:
                    this.ad.style.top = 0;
                    this.ad.style.left = (winSize.windowWidth - this.style.width) / 2;
                    break;
                case SDKDir.XY:
                    this.ad.style.top = this.style.top// * winSize.windowHeight;
                    this.ad.style.left = this.style.left//this.rx * winSize.windowWidth;
                    break;
            }

        }
    }

    getStyle() {
        return {
        }
    }

    create() {
        this.destroy();
        console.log('OppoBannerAd this.adUnitID ', this.adUnitID)
        if (!this.ad) {
            this.ad = this.sdk.createBannerAd({
                adUnitId: this.adUnitID,
                style: this.getStyle()
            })
            this.ad.onLoad(this.onLoad.bind(this))
            this.ad.onError(this.onError.bind(this))
            this.ad.onResize(this.onResize.bind(this))
        }

    }

    destroy(): void {
        console.log(' banner destroy', this.ad)
        if (this.ad) {
            this.ad.offLoad(this.getFunc(this.onLoad))
            this.ad.offError(this.getFunc(this.onError))
            this.ad.offResize(this.onResize.bind(this))
            this.ad.destroy();
            this.ad = null;
        }
    }



    onResize(data: { width: number, height: number }) {
        // console.log(' banner onResize ', data)
        this.style.width = data.width;
        this.style.height = data.height;
        this.updateSize()
    }

}